package yt.demo.appcorequartz.scheduler;


import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.quartz.JobExecutionContext;
import org.springframework.stereotype.Component;
import yt.demo.appcommontools.utils.StringUtils;
import yt.demo.appcorequartz.entity.QuartzJob;
import yt.demo.appcorequartz.entity.QuartzLog;
import yt.demo.appcorequartz.enums.LogStatus;
import cn.hutool.http.HttpRequest;
import yt.demo.appcorequartz.service.IQuartzLogService;

import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Map;

@Slf4j
@Component
public class QuartzRecord extends QuartzJobBean {

    @Autowired
    IQuartzLogService quartzLogService;

    @Override
    protected void executeInternal(JobExecutionContext context) {
        QuartzJob quartzJob = (QuartzJob) context.getMergedJobDataMap().get(QuartzJob.JOB_PARAM_KEY);
        QuartzLog quartzLog = new QuartzLog();
        quartzLog.setJobId(quartzJob.getId());
        quartzLog.setBeanName(quartzJob.getBeanName());
        quartzLog.setParams(quartzJob.getParams());
        long beginTime = System.currentTimeMillis();
        String result = "";
        String params = quartzJob.getParams();
        String beanName =  quartzJob.getBeanName();
        try {
            switch (quartzJob.getType()) {
                case 1://命令任务
                    result = RuntimeUtil.execForStr(beanName);
                    break;
                case 2://类任务
                    Object target = SpringContextUtil.getBean(beanName);
                    Method method = target.getClass().getDeclaredMethod("run", String.class);
                    method.invoke(target, params);
                    break;
                case 3://URL任务
                    System.out.println("getParams = " +quartzJob.getParams());
                    if (params.isEmpty()) {
                        result = HttpUtil.get(beanName);
                    }else{
                        if (StringUtils.isJson(params)) {
                            result = HttpRequest.post(beanName)
                                    .header("Content-Type", "application/json").body(params)
                                    .execute()
                                    .body();
                        }else{
                            Map<String, Object> paramMap = StringUtils.splitToMap(params, ",", "=");
                            result = HttpUtil.get(beanName, paramMap);
                        }
                    }
                    break;
                default:
                    return;
            }
            long executeTime = System.currentTimeMillis() - beginTime;
            quartzLog.setResult(result);
            quartzLog.setExecuteTime((int) executeTime / 1000);
            quartzLog.setStatus(LogStatus.LOG_SUS.getStatus());
        } catch (Exception e) {
            long executeTime = System.currentTimeMillis() - beginTime;
            quartzLog.setExecuteTime((int) executeTime / 1000);
            quartzLog.setStatus(LogStatus.LOG_FAIL.getStatus());
            quartzLog.setError(e.getMessage());
        } finally {
            // 保存执行日志
            quartzLogService.create(quartzLog);
        }
    }


}
